package org.wzq.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wzq.biz.Section;

import java.util.List;

/**
 * project: spring-mvc-forum
 * ate: 14-9-17
 * author: wzq
 * description: TODO
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class SectionDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public List<Section> getSectionAll() {
        Session session = sessionFactory.getCurrentSession();
        List<Section> list = session.createQuery("from Section").list();
        return list;
    }

    @Transactional
    public void saveOrUpdate(Section section) {
        sessionFactory.getCurrentSession().saveOrUpdate(section);
    }


    @Transactional
    public Section getSectionById(String sectionId) {
        String hql = "from Section where sectionId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, sectionId);
        return (Section) query.uniqueResult();
    }

    @Transactional
    public void deleteSection(String sectionId) {
        String hql = "delete from Section where id=?";
        Query q = sessionFactory.getCurrentSession().createQuery(hql);
        q.setString(0, sectionId);
        q.executeUpdate();
    }
}
